package cmd

import (
	"ReaderInOutLog/logger"
	"bytes"
	"encoding/json"
	"io"
	"net/http"
)

var lg, _ = logger.ConfigureLogger()

func GetBase(url string) *[]byte {
	resp, err := http.Get(url)
	if err != nil {
		lg.Errorf("GetBase Error making HTTP request: %v", err)
		return nil
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		lg.Errorf("GetBase Error reading HTTP response: %v", err)
		return nil
	}
	return &body
}

func PostBase(url string, data interface{}) *[]byte {

	jsonData, err := json.Marshal(data)
	if err != nil {
		lg.Errorf("PostBase Error marshaling data: %v", err)
		return nil
	}

	resp, err := http.Post(url, "application/json", bytes.NewBuffer(jsonData))
	if err != nil {
		lg.Errorf("PostBase Error sending HTTP request: %v", err)
		return nil
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		lg.Errorf("PostBase Error reading response body: %v", err)
		return nil
	}

	return &body
}
